package com.kawins.mybatis;


import com.kawins.base.BaseMapper;
import com.kawins.spring.config.EnvConstant;
import com.kawins.spring.config.ConfigKit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;


/**
 * @author rankai
 *         createTime 2017-09-2017/9/27 16:39
 */
public class MapperScanner extends tk.mybatis.spring.mapper.MapperScannerConfigurer {

    private static final Logger log = LoggerFactory.getLogger(MapperScanner.class);

    private static final String ORACLE_IDENTITY = "SELECT UUID FROM DUAL";

    private static final String MYSQL_IDENTITY = "SELECT UUID()";

    private static final Class<?> SUPER_CLASS = BaseMapper.class;

    private String profile;

    public void buildProperties() {
        profile = StringUtils.isBlank(profile) ? ConfigKit.ENV : profile;
        super.setMarkerInterface(SUPER_CLASS);
        Properties properties = new Properties();
        properties.setProperty("mapper", "tk.mybatis.mapper.common.Mapper");
        properties.setProperty("ORDER", "BEFORE");
        if (EnvConstant.DEV_MYSQL.equals(profile) || EnvConstant.TEST_MYSQL.equals(profile) ||
                EnvConstant.PRO_MYSQL.equals(profile) || mysqlProfile(profile)) {
            properties.setProperty("IDENTITY", MYSQL_IDENTITY);
        } else if (EnvConstant.DEV_ORACLE.equals(profile) || EnvConstant.TEST_ORACLE.equals(profile) ||
                EnvConstant.PRO_ORACLE.equals(profile) || oracleProfile(profile)) {
            properties.setProperty("IDENTITY", ORACLE_IDENTITY);
        } else {
            log.warn("no set identity property profile is {}", profile);
        }
        super.setProperties(properties);
    }

    protected boolean mysqlProfile(String profile) {
        return false;
    }

    protected boolean oracleProfile(String profile) {
        return false;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Override
    public void setMarkerInterface(Class<?> superClass) {

    }
}
